﻿#ifndef __MONGODB_BULK_TASK_HEAD__
#define __MONGODB_BULK_TASK_HEAD__

#include <string>
#include <vector>
#include <functional>
#include "multi_task/task.h"

//MongoDbBulkTask任务， 保持同一数据库同一张表，按序执行数据库更改操作，且不失性能

struct S_RedisDataType;

class MongoDbBulkTask : public MultiTask::TaskBase
{
public:
	MongoDbBulkTask(const std::string &instName, const std::string &dbName, const std::string &collectionName, unsigned type, const std::string &queryString, const std::string &docString);
	~MongoDbBulkTask();

public:
	const char *GetName() override { return m_taskInstName.c_str(); }
	void Process(void *sharedObj) override;
	void OnCompleted() override;
	void *CreateSharedObj(unsigned index) override;
	void Go() override;

private:
	std::string m_instName;
	std::string m_taskInstName;
	std::string m_dbName;
	std::string m_collectionName;
	unsigned m_type;
	std::string m_queryString;
	std::string m_docString;
	std::string m_id;
	bool m_bFirst;

	using DataSetType = std::vector<S_RedisDataType>;
	std::shared_ptr<DataSetType> m_datas;

public:
	// for test
	using BulkCBType = std::function<void()>;
	std::shared_ptr<MultiTask::TaskBase> SetBulkCB(const BulkCBType & cb) { m_cb = cb;return shared_from_this(); }
private:
	BulkCBType m_cb;
};

class MongoDbBulkTask_Remove : public MongoDbBulkTask
{
public:
	MongoDbBulkTask_Remove(const std::string &instName, const std::string &dbName, const std::string &collectionName, const std::string &queryString);
};

class MongoDbBulkTask_Insert : public MongoDbBulkTask
{
public:
	MongoDbBulkTask_Insert(const std::string &instName, const std::string &dbName, const std::string &collectionName, const std::string &docString);
};

class MongoDbBulkTask_Update : public MongoDbBulkTask
{
public:
	MongoDbBulkTask_Update(const std::string &instName, const std::string &dbName, const std::string &collectionName, const std::string &queryString, const std::string &docString);
};

#define MongoRemove(...) std::make_shared<MongoDbBulkTask_Remove>(__VA_ARGS__)
#define MongoInsert(...) std::make_shared<MongoDbBulkTask_Insert>(__VA_ARGS__)
#define MongoUpdate(...) std::make_shared<MongoDbBulkTask_Update>(__VA_ARGS__)

#endif  //__MONGODB_BULK_TASK_HEAD__
